package Demojava.Demo01;

import java.io.IOError;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DemoThread extends Thread {
    int ticket = 100;
    Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            sellTicket2();
        }
    }

    public DemoThread(String name) {
        super(name);
    }

    public DemoThread() {
    };

    public boolean sellTicket2() {
        Lock l = new ReentrantLock();
        l.lock();

        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + " is sell " + ticket);
            ticket--;
        } else {
            l.unlock();
            return true;
        }
        l.unlock();
        return true;

    }

    public synchronized boolean sellTicket() {
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + " is sall " + ticket);
            ticket--;
        } else
            return true;
        System.out.println("will return true");
        return true;
    }
}
